package com.sls.web.manager.game.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.EnfuInfo;
import com.sls.web.manager.game.querybean.EnfuQuery;
import com.sls.web.manager.game.service.EnfuService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Repository("enfuService")
public class EnfuServiceImpl extends SuperService implements EnfuService {

	private static Logger log = Logger.getLogger(EnfuServiceImpl.class);
	private Gson gson;

	public EnfuServiceImpl() {
		gson = CommonUtil.gson();
	}

	/**
	 * 审核
	 * 
	 * @param suppliersId
	 * @param priceRatio
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean confirmAudit(String id, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021021");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		EnfuInfo info = new EnfuInfo();
		info.setId(id);
		info.setIsAudit("1");
		info.setOperator(userInfoVO.getUserName());

		SocketDTO dto = super.send(head, info);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 分页查询
	 * 
	 * @param enfuQuery
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public Pagination<EnfuInfo> queryByPage(EnfuQuery enfuQuery,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter EnfuService queryByPage method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ENFU_GOODS_BY_PAGE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, enfuQuery);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			Pagination<EnfuInfo> enfuInfo = gson.fromJson(body,
					new TypeToken<Pagination<EnfuInfo>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave EnfuService queryByPage method...");

			return enfuInfo;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 单条查询--根据商品编号
	 * 
	 * @param id
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public EnfuInfo queryInfoById(String goodsId, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter EnfuService queryInfoById method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ENFU_GOODS_INFO_DETAIL);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		EnfuInfo enfuInfo = new EnfuInfo();
		// enfuInfo.setSupProductId(goodsId);
		SocketDTO dto = super.send(head, enfuInfo);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			EnfuInfo info = gson.fromJson(dto.getBody(), EnfuInfo.class);
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave EnfuService queryInfoById method...");

			return info;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 合作方编号查询
	 * 
	 * @param enfuInfo
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<EnfuInfo> queryMerchantid(UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ENFU_USER_NO);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			EnfuInfo enfuInfo = gson.fromJson(dto.getBody(), EnfuInfo.class);
			List<EnfuInfo> list = new ArrayList<EnfuInfo>();
			list.add(enfuInfo);
			return list;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 余额查询
	 * 
	 * @param enfuQuery
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public EnfuInfo queryBalance(EnfuQuery enfuQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ENFU_USER_BALANCE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, enfuQuery);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body) && !body.equals("\"\"")) {
				EnfuInfo enfuInfo = gson
						.fromJson(dto.getBody(), EnfuInfo.class);
				return enfuInfo;
			} else {
				throw new ManagerException("未知错误！");
			}
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 订单状态查询
	 * 
	 * @param enfuQuery
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public EnfuInfo queryOrderStatus(EnfuQuery enfuQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ENFU_ORDER_STATUS_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, enfuQuery);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body) && !body.equals("\"\"")) {
				EnfuInfo enfuInfo = gson.fromJson(body, EnfuInfo.class);
				return enfuInfo;
			} else {
				throw new ManagerException("未知错误！");
			}
		}
		return null;
	}

	/**
	 * 同步商品详细
	 * 
	 * @param userInfoVO
	 * @throws Exception
	 */
	@Override
	public void querygoodsDetailSync(UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020992");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		EnfuInfo enInfo = new EnfuInfo();
		enInfo.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, enInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 批量上架
	 * 
	 * @param goodsState
	 * @param userInfoVO
	 * @throws Exception
	 */
	@Override
	public void batchOnline(String goodsState, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021023");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		EnfuInfo info = new EnfuInfo();
		info.setGoodsState(goodsState);
		info.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, info);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 批量下架
	 * 
	 * @param goodsState
	 * @param userInfoVO
	 * @throws Exception
	 */
	@Override
	public void batchOffline(String goodsState, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021023");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		EnfuInfo info = new EnfuInfo();
		info.setGoodsState(goodsState);
		info.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, info);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 商品批量同步
	 * 
	 * @param userInfoVO
	 * @throws Exception
	 */
	@Override
	public void queryGoodsSync(UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021012");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		EnfuQuery enfuQuery = new EnfuQuery();
		enfuQuery.setOperator(userInfoVO.getUserName());

		SocketDTO dto = super.send(head, enfuQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 分页查询--商品详细
	 * 
	 * @param enfuQuery
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public Pagination<EnfuInfo> queryByPageOfGoodsDetail(EnfuQuery enfuQuery,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter EnfuService queryByPage method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021020");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, enfuQuery);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			Pagination<EnfuInfo> enfuInfo = gson.fromJson(body,
					new TypeToken<Pagination<EnfuInfo>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave EnfuService queryByPage method...");

			return enfuInfo;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 分页查询--商品大区分页展示
	 * 
	 * @param enfuQuery
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public Pagination<EnfuInfo> queryByPageOfArea(EnfuQuery enfuQuery,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter EnfuService queryByPage method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021024");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, enfuQuery);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			Pagination<EnfuInfo> enfuInfo = gson.fromJson(body,
					new TypeToken<Pagination<EnfuInfo>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave EnfuService queryByPage method...");

			return enfuInfo;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 商品小服，分页展示
	 * 
	 * @param enfuQuery
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public Pagination<EnfuInfo> queryByPageOfSrv(EnfuQuery enfuQuery,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter EnfuService queryByPage method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021025");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, enfuQuery);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			Pagination<EnfuInfo> enfuInfo = gson.fromJson(body,
					new TypeToken<Pagination<EnfuInfo>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave EnfuService queryByPage method...");

			return enfuInfo;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

}
